﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgoImpl
{

    public class SharedData
    {
        private Dictionary<string, Shift> m_AllShifts;
        private Dictionary<string, Worker> m_AllWorkers;


        private static int s_ToInitializeOnlyOnce = 0;
        private static volatile SharedData s_Instance = null;
        private static object s_LockObj = new Object();

        private SharedData() 
        {
            m_AllShifts = new Dictionary<string, Shift>();
            m_AllWorkers = new Dictionary<string, Worker>();
        }

        public void Clear()
        {
            m_AllShifts.Clear();
            m_AllWorkers.Clear();
            s_ToInitializeOnlyOnce = 0;
        }

        public List<string> GetAllShiftsIds()
        {
            return m_AllShifts.Keys.ToList();
        }

        public List<string> GetAllWorkersIds()
        {
            return m_AllWorkers.Keys.ToList();
        }


        public Worker GetWorkerById(string id)
        {
            return m_AllWorkers[id];
        }

        public Shift GetShiftById(string id)
        {
            return m_AllShifts[id];
        }

        public int GetNumOfShifts()
        {
            return m_AllShifts.Count;
        }

        public int GetNumOfWorkers()
        {
            return m_AllWorkers.Count;
        }

        public static SharedData Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_LockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new SharedData();
                        }

                    }
                }

                return s_Instance;
            }
        }

        public void InitializeWorkersAndShift(List<Shift> allShifts, List<Worker> allWorkers)
        {
            if (s_ToInitializeOnlyOnce == 0)
            {
                setAsDictionaryWorkersById(allWorkers);
                setAsDictionaryShiftsById(allShifts);
                s_ToInitializeOnlyOnce++;
            }
        }

        private void setAsDictionaryShiftsById(List<Shift> allShifts)
        {
            foreach (Shift shift in allShifts)
            {
                m_AllShifts.Add(shift.Id, shift);
            }
        }

        private void setAsDictionaryWorkersById(List<Worker> allWorkers)
        {
            foreach (Worker worker in allWorkers)
            {
                m_AllWorkers.Add(worker.Id, worker);
            }
        }
    }
}
